package com.hy.erp.inventory.service;

import java.util.ArrayList ;
import java.util.List ;
import java.util.Map ;

import javax.ejb.Stateless ;

import org.jboss.seam.ScopeType ;
import org.jboss.seam.annotations.Name ;
import org.jboss.seam.annotations.Scope ;

import com.hy.enterprise.architecture.foundation.ArchitectureFoundationException;
import com.hy.enterprise.framework.service.business.AbstractBusinessService;
import com.hy.erp.inventory.dao.interfaces.IMaterielDao;
import com.hy.erp.inventory.pojo.Materiel;
import com.hy.erp.inventory.pojo.ProductCharacteristic;
import com.hy.erp.inventory.pojo.Supplier;
import com.hy.erp.inventory.pojo.interfaces.IMateriel;
import com.hy.erp.inventory.service.interfaces.IMaterielService;
import com.hy.erp.inventory.vsoft.libra.enterprise.framework.persistence.entity.AbstractPersistentEntity;
import com.hy.erp.inventory.vsoft.libra.enterprise.framework.util.lang.UUIDUtil;

@ Name ( "materielService" )
@ Scope ( ScopeType.STATELESS )
@ Stateless
public class MaterielService extends AbstractBusinessService < IMateriel > implements IMaterielService
{

	@ Override
	public boolean addMateriel (
									IMateriel materiel )
	{


		if ( null == materiel )
		{
			return true ;
		}

		else
		{
			IMaterielDao materielDao =
												( IMaterielDao ) this
																			.getDao ( IMateriel.class ) ;

			return materielDao
									.persist ( materiel ) ;
		}

	
	}

	@ Override
	public boolean modifyMateriel (
									IMateriel materiel )
	{


		if ( ( null == materiel )
				|| ( null == materiel
											.getIdentifier ( ) )
				|| ( materiel
									.getIdentifier ( )
									.trim ( )
									.length ( ) == 0 ) )
		{
			return true ;
		}
		else
		{

			IMaterielDao materielDao =
				( IMaterielDao ) this
											.getDao ( IMateriel.class ) ;


			if ( null == materiel )
			{
				throw new ArchitectureFoundationException (
															"修改实体数据时发生错误，所需要修改的实体在数据库中并不存在" ) ;
			}

			return ( materielDao
									.merge ( materiel ) != null ) ;
		}

	
	}

	@ Override
	public Integer removeMateriel (
									String [ ] materielUseIds )
	{
		if ( ( null == materielUseIds )
				|| ( materielUseIds.length == 0 ) )
		{
			return new Integer (
									0 ) ;
		}
		else
		{

			IMaterielDao materielDao =
				( IMaterielDao ) this
											.getDao ( IMateriel.class ) ;

			return materielDao
									.remove ( materielUseIds ) ;
		}
	}

	@ Override
	public List < Materiel > findByNamedQuery (
												String queryName ,
												Map < String , Object > parameters )
	{

		if ( null ==queryName &&null == parameters )
		{
			return null ;
		}
		else
		{
	
			IMaterielDao materielDao =
				( IMaterielDao ) this
											.getDao ( IMateriel.class ) ;

			return materielDao
			                .findByNamedQueryAndNamedParams (
			                                                  queryName,
			                                                  parameters) ;
		

	}


	}

	@ Override
	public Materiel findUniqueResultByNamedQuery (
													String queryName ,
													Map < String , Object > parameters )
	{

		if ( null ==queryName &&null == parameters )
		{
			return null ;
		}
		else
		{
			IMaterielDao materielDao =
				( IMaterielDao ) this
											.getDao ( IMateriel.class ) ;

			return materielDao
			                .findUniqueResultByNamedParams (
			                                                  queryName,
			                                                  parameters ) ;
	}
		
	}

	@ Override
	public String [ ] addMateriel (
									Object [ ] materiel )
	{



		if ( null == materiel )
		{
			return null ;
		}

		else
		{
			
			IMaterielDao materielDao =
												( IMaterielDao ) this
																			.getDao ( IMateriel.class ) ;

			Materiel[] materiels=new Materiel[materiel.length];
						for(int i=0;i<materiel.length;i++)
						{
							Materiel type=( Materiel ) materiel[i];
							
							List < Supplier> suppliers=new ArrayList < Supplier > ( );	
			            	List < ProductCharacteristic> productCharacteristics=new ArrayList < ProductCharacteristic > ( );
			            	
			            	if(type.getCharacteristic ( )  !=null)
							{
			            		ProductCharacteristic ed =
									null;
								productCharacteristics=type.getCharacteristic ( );
								
								for(int j=0;j<productCharacteristics.size ( );j++)
								{
									ed=new ProductCharacteristic();
							 ed= productCharacteristics.get ( j );
							 if(ed.getIdentifier ( )==null)
			     			 {
			     			 String	uuid = UUIDUtil
			     		        .randomUUID ( );
			     			ed.setIdentifier ( uuid );
			     			
			     			}	
							}
								type.setCharacteristic    ( productCharacteristics );
							}
			            	
			            	if(type.getSupplier    ( )!=null)
							{
			            		Supplier ed =
									null;
			            		suppliers=type.getSupplier ( );
								
								for(int j=0;j<suppliers.size ( );j++)
								{
									ed=new Supplier();
							 ed=suppliers.get ( j );
							 if(ed.getIdentifier ( )==null)
			     			 {
			     			 String	uuid = UUIDUtil
			     		        .randomUUID ( );
			     			ed.setIdentifier ( uuid );
			     			
			     			}	
							}
								type.setSupplier    ( suppliers );
							}
							materiels[i]=type;
						}
						return materielDao
						                .persist ( materiels ) ;
			
		}

	
	
	}

	@ Override
	public List < IMateriel > getAllMateriel ( )
	{
		IMaterielDao materielDao =
			( IMaterielDao ) this
										.getDao ( IMateriel.class ) ;

		return materielDao
		                .find ( );
	}
	
	@ Override
	public List < IMateriel > getMaterielsByIds (
													String [ ] Ids )
	{
		if ( ( null == Ids )
			     || ( Ids.length == 0 ) )
			{
				return null ;
			}
			else
			{
				IMaterielDao materielDao =
					( IMaterielDao ) this
												.getDao ( IMateriel.class ) ;
	                                        
				return materielDao.findByIdentifier ( Ids );
				
			}
	}

	@ Override
	public  IMateriel  getMaterielsById (
													String id )
	{
		if (  null == id )
			{
				return null ;
			}
			else
			{
				IMaterielDao materielDao =
					( IMaterielDao ) this
												.getDao ( IMateriel.class ) ;
	                                        
				return materielDao.findByIdentifier ( id );
				
			}
	}

	@ Override
	public IMateriel getMaterielsById (
										AbstractPersistentEntity baseModel )
	{
		if (  null ==baseModel )
		{
			return null ;
		}
		else
		{
			String id =baseModel.get ( "identifier" );
		
			
			IMaterielDao materielDao =
				( IMaterielDao ) this
											.getDao ( IMateriel.class ) ;
                                        
			return materielDao.findByIdentifier ( id );
			
		}
	}

	@ Override
	public String [ ] modifyMateriel (
										Object [ ] materiel )
	{




		if ( null == materiel )
		{
			return null ;
		}

		else
		{
			
			IMaterielDao materielDao =
												( IMaterielDao ) this
																			.getDao ( IMateriel.class ) ;

			Materiel[] materiels=new Materiel[materiel.length];
						for(int i=0;i<materiel.length;i++)
						{
							Materiel type=( Materiel ) materiel[i];
							
							
							List < Supplier> suppliers=new ArrayList < Supplier > ( );	
			            	List < ProductCharacteristic> productCharacteristics=new ArrayList < ProductCharacteristic > ( );
			            	
			            	if(type.getCharacteristic ( )  !=null)
							{
			            		ProductCharacteristic ed =
									null;
								productCharacteristics=type.getCharacteristic ( );
								
								for(int j=0;j<productCharacteristics.size ( );j++)
								{
									ed=new ProductCharacteristic();
							 ed= productCharacteristics.get ( j );
							 if(ed.getIdentifier ( )==null)
			     			 {
			     			 String	uuid = UUIDUtil
			     		        .randomUUID ( );
			     			ed.setIdentifier ( uuid );
			     			
			     			}	
							}
								type.setCharacteristic    ( productCharacteristics );
							}
			            	
			            	if(type.getSupplier    ( )!=null)
							{
			            		Supplier ed =
									null;
			            		suppliers=type.getSupplier ( );
								
								for(int j=0;j<suppliers.size ( );j++)
								{
									ed=new Supplier();
							 ed=suppliers.get ( j );
							 if(ed.getIdentifier ( )==null)
			     			 {
			     			 String	uuid = UUIDUtil
			     		        .randomUUID ( );
			     			ed.setIdentifier ( uuid );
			     			
			     			}	
							}
								type.setSupplier    ( suppliers );
							}
							
							materiels[i]=type;
						}
						return materielDao
						                .merge ( materiels ) ;
			
		}

	
	
	
	}
}
